package org.looop.studio.noots;

import junit.framework.TestCase;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class BaseTestCase extends TestCase {
	
	protected String[] titles = new String[]{"Thinking in java", "lucene in action", "java concurrent programing in practice", "Something"};
	protected String[] pubmonth = new String[]{"199501", "199901", "200506", "201212"};
	protected String[] category = new String[]{"java", "search engine", "concurrency", "in mind"};
	
	protected String getIndexDir() {
		String indexDir = getTmpDir() + "index-dir";
		return indexDir;
	}
	
	public BaseTestCase() {
		super();
	}

	protected String getTmpDir(){
		return System.getProperty("java.io.tmpdir", "tmp") + System.getProperty("file.separator");
	}
	
	protected Directory createBookIndex() throws Exception {
		Directory directory = new RAMDirectory();
		IndexWriter writer = new IndexWriter(directory, getAnalyzer(), true);
		writer.setUseCompoundFile(isCompund());
		for(int i=0; i < titles.length; i++){
			Document doc = new Document();
			doc.add(Field.Text("title", titles[i]));
			doc.add(Field.Text("pubmonth", pubmonth[i]));
			doc.add(Field.Text("category", category[i]));
			writer.addDocument(doc);
		}
		writer.optimize();
		writer.close();
		return directory;
	}
	
	protected Directory createSingleFieldIndex(String field, String[] contents, Analyzer analyzer) throws Exception {
		Directory directory = new RAMDirectory();
		IndexWriter writer = new IndexWriter(directory, getAnalyzer(), true);
		writer.setUseCompoundFile(isCompund());
		for(int i=0; i < contents.length; i++){
			Document doc = new Document();
			doc.add(Field.Text(field, contents[i]));
			writer.addDocument(doc);
		}
		writer.optimize();
		writer.close();
		return directory;
	}
	
	protected boolean isCompund() {
		return true;
	}
	
	protected Analyzer getAnalyzer() {
		return new SimpleAnalyzer();
	}
}
